package me.zhengjie.modules.customer.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.customer.domain.TCustomerInfo;
import me.zhengjie.modules.customer.service.TCustomerContractService;
import me.zhengjie.modules.customer.service.TCustomerInfoService;
import me.zhengjie.modules.customer.service.dto.TCustomerInfoQueryCriteria;
import me.zhengjie.modules.workorder.service.TAccidentService;
import me.zhengjie.utils.HttpResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
* @author xuyukun
* @date 2019-07-21
*/
@RestController
@RequestMapping("api")
public class TCustomerInfoController {

    @Autowired
    private TCustomerInfoService tCustomerInfoService;

    @Log("查询TCustomerInfo")
    @GetMapping(value = "/tCustomerInfo")
    @PreAuthorize("hasAnyRole('ADMIN','TCUSTOMERINFO_ALL','TCUSTOMERINFO_SELECT')")
    public ResponseEntity getTCustomerInfos(TCustomerInfoQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity(tCustomerInfoService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("查询TCustomerInfo")
    @GetMapping(value = "/tCustomerInfo/all")
    @PreAuthorize("hasAnyRole('ADMIN','TCUSTOMERINFO_ALL','TCUSTOMERINFO_SELECT')")
    public ResponseEntity getTCustomerInfoAll(TCustomerInfoQueryCriteria criteria){
        return new ResponseEntity(tCustomerInfoService.queryAll(criteria),HttpStatus.OK);
    }


    @Log("查询TCustomerInfo")
    @GetMapping(value = "/tCustomerInfoAll")
    @PreAuthorize("hasAnyRole('ADMIN','TCUSTOMERINFO_ALL','TCUSTOMERINFO_SELECT')")
    public ResponseEntity tCustomerInfoAll(){
        TCustomerInfoQueryCriteria criteria =new TCustomerInfoQueryCriteria();
        return new ResponseEntity(tCustomerInfoService.queryAll(criteria),HttpStatus.OK);
    }
    @Log("新增TCustomerInfo")
    @PostMapping(value = "/tCustomerInfo")
    @PreAuthorize("hasAnyRole('ADMIN','TCUSTOMERINFO_ALL','TCUSTOMERINFO_CREATE')")
    public ResponseEntity create(@Validated @RequestBody TCustomerInfo resources){
        return new ResponseEntity(tCustomerInfoService.create(resources),HttpStatus.CREATED);
    }

    @Log("修改TCustomerInfo")
    @PutMapping(value = "/tCustomerInfo")
    @PreAuthorize("hasAnyRole('ADMIN','TCUSTOMERINFO_ALL','TCUSTOMERINFO_EDIT')")
    public ResponseEntity update(@Validated @RequestBody TCustomerInfo resources){
        tCustomerInfoService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @Log("删除TCustomerInfo")
    @DeleteMapping(value = "/tCustomerInfo/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','TCUSTOMERINFO_ALL','TCUSTOMERINFO_DELETE')")
    public ResponseEntity delete(@PathVariable Long id){
        try {
            tCustomerInfoService.delete(id);
            return new ResponseEntity(HttpStatus.OK);
        }catch(Exception ex) {
            ex.printStackTrace();
            return HttpResponseUtils.passDefaultResponse(ex);
        }
    }
}